﻿using System;
using System.Windows.Input;

namespace ClientLauncher.Src.WPFUtils
{
  public class RelayCommand : ICommand
  {
    private readonly Predicate<object> myCanExecute;
    private readonly Action<object> myExecute;

    public RelayCommand(Action<object> execute, Predicate<object> canExecute = null)
    {
      if (execute == null)
        throw new ArgumentNullException("execute");

      myExecute = execute;
      myCanExecute = canExecute;
    }

    #region ICommand Members

    public bool CanExecute(object parameter)
    {
      return myCanExecute == null || myCanExecute(parameter);
    }

    public event EventHandler CanExecuteChanged
    {
      add { CommandManager.RequerySuggested += value; }
      remove { CommandManager.RequerySuggested -= value; }
    }

    public void Execute(object parameter)
    {
      myExecute(parameter);
    }

    #endregion
  }
}